/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  10:05:22 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.entitys.common
 * File Name : JPAService.java
 * TODO
 */
package jpa.service.common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.construction.PurchaseOrderPK;
import promis.utility.CommonService;
import sap.jco.server.SAPServerInfo;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 11:47:03 AM 
 * CreateBy=Chatree Siangklom
 * 
 * JPAService
 * TODO
 */
public class JPAService implements JPAServiceIntrerface {
	
	private static final Logger log = Logger.getLogger(JPAService.class);
	private EntityManagerFactory emf;
	
	@PersistenceContext
	private EntityManager em;	
	
	private String user = SAPServerInfo.User();
	private Date current_date = Calendar.getInstance().getTime();
	
	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @return the current_date
	 */
	public Date getCurrent_date() {
		return current_date;
	}
	
	public JPAService(EntityManager em){
		this.em = em;		
	}
	
	/**
	 * @return the emf
	 */
	protected EntityManagerFactory getEmf() {
		return emf;
	}

	/**
	 * @param emf the emf to set
	 */
	public void setEmf(EntityManagerFactory emf) {
		this.emf = emf;
	}

	/**
	 * @return the em
	 */
	protected EntityManager getEm() {
		return em;
	}

	/**
	 * @param em the em to set
	 */
	public void setEm(EntityManager em) {
		this.em = em;
	}	
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:21 AM 
	 * CreateBy=Chatree Siangklom
	 * T
	 * TODO
	 * @param object
	 * @return
	 * @throws Exception
	 */
	protected <T> T ConvertToYoureObject(CommonEntity object) throws Exception {
		System.out.println("object : " + object);
		try{
			
			if(!CommonService.IsNull((Object) object)) 
				return (T)object;
			else 
				return null;	
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}		
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:25 AM 
	 * CreateBy=Chatree Siangklom
	 * List<T>
	 * TODO
	 * @param obj_lst
	 * @return
	 * @throws Exception
	 */
	public <T> List<T> ConvertToYoureObject(List<CommonEntity> obj_lst) throws Exception {
		try{
			if(CommonService.getSize(obj_lst) <= 0) 
				return null;
			
			List<T> newlst = new ArrayList<T>();
			for(CommonEntity model : obj_lst) 
				newlst.add((T)model);
			
			return newlst;
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#find(java.lang.Object, java.lang.String)
	 * Date =Nov 12, 2013 : 11:29:19 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO { Need Name is Id }
	 */
	public CommonEntity find(Object id, String NamedQuery) throws Exception {	
		EntityTransaction t = null;
		try{
			
	        t = this.em.getTransaction();	        	        
	        t.begin();

	        Query query = this.em.createNamedQuery(NamedQuery);	    
	        query.setParameter("Id", id);	        
	        
	        List lst = query.getResultList();	        
	        t.commit();		
	        //em.flush();
	        
	        if(CommonService.getSize(lst) > 0)
        		return (CommonEntity)lst.get(0);
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//NamedQuery="+ NamedQuery);      
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
		return null;
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:31 AM 
	 * CreateBy=Chatree Siangklom
	 * List<CommonEntity>
	 * TODO
	 * @param instanceNo
	 * @param NamedQuery
	 * @return
	 * @throws Exception
	 */
	public List<CommonEntity> findByInstanceNo(String instanceNo, String NamedQuery) throws Exception {
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        Query query = this.em.createNamedQuery(NamedQuery);	    
	        query.setParameter("instanceNo", instanceNo);
	        
        	List lst = query.getResultList();
        	t.commit();
        	//em.flush();
        	
	        if(CommonService.getSize(lst) > 0)	
	        	return lst;
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//NamedQuery="+ NamedQuery + ",instanceNo=" + instanceNo);     
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
		return null;
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#find(java.lang.Object)
	 * Date =Nov 12, 2013 : 11:29:44 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public CommonEntity find(Object id) throws Exception {
		return null;
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#find(java.lang.String, java.util.Map)
	 * Date =Nov 12, 2013 : 11:29:53 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public List<CommonEntity> find(String NamedQuery, Map<String, Object> mapWhereValue) throws Exception {
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        Query query = this.em.createNamedQuery(NamedQuery);
	        if(!mapWhereValue.isEmpty())
	        {
	        	Set s = mapWhereValue.entrySet();
	        	Iterator it=  s.iterator();
	        	while(it.hasNext())
    	        {	            
    	            Map.Entry m =(Map.Entry)it.next();    	            
    	            query.setParameter((String)m.getKey(), m.getValue());    	           
    	        }
	        }	        

        	List lst = query.getResultList();
        	t.commit();
        	//em.flush();
        	
	        return lst;        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//NamedQuery="+ NamedQuery);  
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }	
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:40 AM 
	 * CreateBy=Chatree Siangklom
	 * List<CommonEntity>
	 * TODO
	 * @param NamedQuery
	 * @return
	 * @throws Exception
	 */
	protected List<CommonEntity> findAll(String NamedQuery) throws Exception {
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        Query query = this.em.createNamedQuery(NamedQuery);	        	      

	        List lst = query.getResultList();
	        t.commit();
	        //em.flush();
	        
	        return lst;	
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//NamedQuery="+ NamedQuery);    
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}

	/**
	 * 
	 * @param company_name is case sensitive
	 * @param sequence_name is case sensitive
	 * @return
	 */
	protected synchronized String SequenceNext(String sequence_name) throws Exception {
		EntityTransaction t = null;
		try{
			t = this.em.getTransaction();
	        t.begin();
	        
        	Query query = em.createNativeQuery("SELECT SPL.GETFN_SYNC_SEQUENCE(?1) FROM DUAL");
        	query.setParameter(1, sequence_name);		//log.info("sequence_name : " + sequence_name);
        	Object obj = query.getSingleResult();
        	t.commit();
        	//em.flush();
        	
        	return CommonService.IsNullRString(obj);
        	
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//GETFN_SYNC_SEQUENCE(sequence_name="+ sequence_name);
		}finally {
			  if (t.isActive()) 
				  t.rollback();
		}
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:44 AM 
	 * CreateBy=Chatree Siangklom
	 * String
	 * TODO
	 * @return
	 * @throws Exception
	 */
	public synchronized String GetNewInstanceNo() throws Exception {
		return SequenceNext("INSTANCE");
	}
	
	/**
	 * 
	 * @param company = Promis Company
	 * @param project = Promis Project
	 * @param type = Promis VOUCHER Type 
	 * @param date = Promis VOUCHER Date format : YYYY-MM-DD
	 * @return
	 * @throws Exception
	 */
	protected synchronized String PromisSeriesNext(String company, String project, String type, String date) throws Exception {
		
		EntityTransaction t = null;
		String current_next = null;
		try{
			
			t = this.em.getTransaction();
	        t.begin();
        	Query query = em.createNativeQuery(" SELECT SPL.GET_VOUCHER_SERIES(?1,?2,?3,?4) FROM DUAL ");
        	
        	query.setParameter(1, company);			//log.info("Company : " + company);
        	query.setParameter(2, type);			//log.info("type : " + type);
        	query.setParameter(3, date);			//log.info("date : " + date);
        	query.setParameter(4, project);			//log.info("project : " + project);
        	
        	Object obj = query.getSingleResult();
        	t.commit();
        	//em.flush();
        	
        	current_next = CommonService.IsNullRString(obj);
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//GET_VOUCHER_SERIES(company="+ company +",type="+ type +",date="+ date +",project="+ project +")//Result Series=" + current_next);
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
		return current_next;
	}
	
	/**
	 * 
	 * @param company = Promis Company
	 * @param project = Promis Project
	 * @param type = Promis VOUCHER Type 
	 * @param date = Promis VOUCHER Date format : YYYY-MM-DD
	 * @return
	 * @throws Exception
	 */
	public String getPromisSeriesNext(String company, String project, String type, String date) throws Exception {
		return PromisSeriesNext(company, project, type, date);
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:48 AM 
	 * CreateBy=Chatree Siangklom
	 * Long
	 * TODO
	 * @param company
	 * @param project
	 * @param type
	 * @param series
	 * @return
	 * @throws Exception
	 */
	protected synchronized Long PromisDocumentNoNext(String company, String project, String type, String series) throws Exception {
		EntityTransaction t = null;
		try{
			t = this.em.getTransaction();
	        t.begin();
	    
        	Query query = em.createNativeQuery(" SELECT SPL.GEN_DOCUMENT_NO(?1,?2,?3,?4) FROM DUAL ");        
        	query.setParameter(1, company);			//log.info("Company : " + company);
        	query.setParameter(2, type);			//log.info("type : " + type);
        	query.setParameter(3, series);			//log.info("series : " + series);
        	query.setParameter(4, project);			//log.info("project : " + project);
        	
        	Object obj = query.getSingleResult();
        	t.commit();
        	//em.flush();
        	
        	return (CommonService.IsNull(obj) ? Long.parseLong("-1") : Long.parseLong(CommonService.IsNullRZero((BigDecimal)obj)));
        	
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//GEN_DOCUMENT_NO(company="+ company +",series="+ series +",type="+ type +",project="+ project +")");
		}finally {
			  if (t.isActive()) 
				  t.rollback();
		}
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:52 AM 
	 * CreateBy=Chatree Siangklom
	 * Long
	 * TODO
	 * @param company
	 * @param project
	 * @param type
	 * @param series
	 * @return
	 * @throws Exception
	 */
	public Long getPromisDocumentNoNext(String company, String project, String type, String series) throws Exception {
		return PromisDocumentNoNext(company, project, type, series);
	}

	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:55 AM 
	 * CreateBy=Chatree Siangklom
	 * long
	 * TODO
	 * @param company
	 * @return
	 * @throws Exception
	 */
	public synchronized long GetNeqOutgoingSeqOnPromis(String company) throws Exception {
		String seqNext = SequenceNextOnPromis(company, "PAYMENT_REQUEST_OUTGOING_SEQ");
		if(!CommonService.IsNull(seqNext)){
			return Long.parseLong(seqNext);
		} throw new Exception("Can't find Promis Sequence on SPL  company = " + company + ", sequence_name = PAYMENT_REQUEST_OUTGOING_SEQ ");
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:31:58 AM 
	 * CreateBy=Chatree Siangklom
	 * String
	 * TODO
	 * @param company
	 * @param sequence_name
	 * @return
	 * @throws Exception
	 */
	protected synchronized String SequenceNextOnPromis(String company, String sequence_name) throws Exception {
		EntityTransaction t = null;
		try{
			t = this.em.getTransaction();
	        t.begin();
	        
        	Query query = em.createNativeQuery("SELECT  SPL.GETFN_SYNC_SEQUENCE(?1,?2) FROM DUAL");
        	query.setParameter(1, company);				//log.info("company : " + company);
        	query.setParameter(2, sequence_name);		//log.info("sequence_name : " + sequence_name);
        	
        	Object obj = query.getSingleResult();
        	t.commit();
        	//em.flush();
        	
        	return (CommonService.IsNull(obj) ? null : CommonService.IsNullRString(obj));
        	
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//GEN_DOCUMENT_NO(company="+ company +",sequence_name="+ sequence_name);
		}finally {
			  if (t.isActive()) 
				  t.rollback();
		}
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:01 AM 
	 * CreateBy=Chatree Siangklom
	 * String
	 * TODO
	 * @param company
	 * @param project
	 * @param voucher_type
	 * @return
	 * @throws Exception
	 */
	public synchronized String MktRunningNon(String company, String project, String voucher_type) throws Exception {
		return MKT_RUNNING_NONextOnPromis(company, project, voucher_type);
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:04 AM 
	 * CreateBy=Chatree Siangklom
	 * String
	 * TODO
	 * @param company
	 * @param project
	 * @param voucher_type
	 * @return
	 * @throws Exception
	 */
	protected synchronized String MKT_RUNNING_NONextOnPromis(String company, String project, String voucher_type) throws Exception {
		EntityTransaction t = null;
		
		try{
			t = this.em.getTransaction();
	        t.begin();
		        
	        	Query query = em.createNativeQuery(" SELECT  SPL.MKT_RUNNING_NO(?1,?2,?3,?4) FROM DUAL ");
	        	query.setParameter(1, company);					//log.info(" company = " + company);
	        	query.setParameter(2, project);					//log.info(" project = " + project);
	        	query.setParameter(3, voucher_type);			//log.info(" voucher_type = " + voucher_type);
	        	query.setParameter(4, null);			
        	
	        	Object obj = query.getSingleResult();
	        	t.commit();
	        	//em.flush();
	        	
	        	return CommonService.IsNullRString(obj);
	        	
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage() + "//GEN_DOCUMENT_NO(company="+ company +",project="+ project + ",voucher_type=" + voucher_type);
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}

	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:08 AM 
	 * CreateBy=Chatree Siangklom
	 * Double
	 * TODO
	 * @param poId
	 * @return
	 * @throws Exception
	 */
	public synchronized Double CheckPoReceived(PurchaseOrderPK poId) throws Exception {
		try{
			
			EntityTransaction t = null;
			try{
				t = this.em.getTransaction();
		        t.begin();
		       	        
	        	Query query = em.createNativeQuery(" SELECT CHECK_PO_RECEIVED(?1,?2,?3,?4,?5) FROM DUAL ");        
	        
	        	query.setParameter(1, poId.getCompany()); 		//log.info("Company : " + poId.getCompany());
	        	query.setParameter(2, poId.getProject());		//log.info("Project : " + poId.getProject());
	        	query.setParameter(3, poId.getPoType());		//log.info("PoType : " + poId.getPoType());
	        	query.setParameter(4, poId.getPoSeries());      //log.info("PoSeries : " + poId.getPoSeries());
	        	query.setParameter(5, poId.getPoNo());			//log.info("PoNo : " + poId.getPoNo());
	        	
	        	Object obj = query.getSingleResult();
	        	t.commit();
	        	//em.flush();
	        	
	        	if(!CommonService.IsNull(obj))	        	
	        		return CommonService.ConvertStringToDouble(CommonService.IsNullRZero((BigDecimal)obj));
	        	else
	        		return Double.parseDouble("0");
	        	
			}catch(Exception e){
	        	e.printStackTrace();
	        	if (t.isActive()) 
					  t.rollback();
	        	throw new Exception("Error:" + e.getMessage() + "CHECK_PO_RECEIVED --> Company="+ poId.getCompany() + ",Project=" + poId.getProject() + ",PoType="+ poId.getPoType() + ",PoSeries=" + poId.getPoSeries() + ",PoNo=" + poId.getPoNo());	
			}finally {
				  if (t.isActive()) 
					  t.rollback();
		    }
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#inserted(jpa.entitys.common.CommonEntity)
	 * Date =Nov 12, 2013 : 11:30:06 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public CommonEntity inserted(CommonEntity object) throws Exception {
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();
	        t.begin();
	        em.persist(object);		       
	        t.commit();
	        //em.flush();
	        
	        return object;
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#inserted(java.util.List)
	 * Date =Nov 12, 2013 : 11:30:30 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public List<CommonEntity> inserted(List<CommonEntity> object_lst) throws Exception {
		if(CommonService.IsNull(object_lst)) return null;
		
		EntityTransaction t = null;
		try{
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        for(CommonEntity obj : object_lst)
	        	em.persist(obj);
	        t.commit();	
	        //em.flush();
	        
	        return object_lst;
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());        
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:12 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param object_lst1
	 * @param object_lst2
	 * @throws Exception
	 */
	public void inserted(List<CommonEntity> object_lst1, List<CommonEntity> object_lst2) throws Exception {
			
		if(CommonService.IsNull(object_lst1) && CommonService.IsNull(object_lst2)) return;
		
		EntityTransaction t = null;
		try{
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        for(CommonEntity obj : object_lst1)
	        	em.persist(obj);
	        
			for(CommonEntity obj : object_lst2)
	        	em.persist(obj);
			
			t.commit();
			//em.flush();
			
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }		
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:15 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param object_lst1
	 * @param object_lst2
	 * @param object_lst3
	 * @throws Exception
	 */
	public void updated(CommonEntity object_lst1, List<CommonEntity> object_lst2, List<CommonEntity> object_lst3) throws Exception {
		
		EntityTransaction t = null;
		
		try{
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        	em.merge(object_lst1);
	        
				for(CommonEntity obj : object_lst2)
		        	em.merge(obj);
					
					for(CommonEntity obj : object_lst3)
						em.merge(obj);
			
			t.commit();
			//em.flush();
			
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:18 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param object_lst1
	 * @param object_lst2
	 * @param object_lst3
	 * @throws Exception
	 */
	public void inserted(CommonEntity object_lst1, List<CommonEntity> object_lst2, List<CommonEntity> object_lst3) throws Exception {
		
		EntityTransaction t = null;
		try{
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        	em.persist(object_lst1);
	        
				for(CommonEntity obj : object_lst2)
		        	em.persist(obj);
					
					for(CommonEntity obj : object_lst3)
						em.persist(obj);
			
			t.commit();	 
			//em.flush();
			
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 11:32:21 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param object_lst1
	 * @param object_lst2
	 * @param object_lst3
	 * @param object_lst4
	 * @throws Exception
	 */
	public void inserted(List<CommonEntity> object_lst1, List<CommonEntity> object_lst2, List<CommonEntity> object_lst3, List<CommonEntity> object_lst4) throws Exception {
		
		if(CommonService.IsNull(object_lst1) && CommonService.IsNull(object_lst2) && CommonService.IsNull(object_lst3)  && CommonService.IsNull(object_lst4)) return;
		
		EntityTransaction t = null;
		try{
	        t = this.em.getTransaction();	        	        
	        t.begin();
	        
	        for(CommonEntity obj : object_lst1)
	        	em.persist(obj);
	        
				for(CommonEntity obj : object_lst2)
		        	em.persist(obj);
					
					for(CommonEntity obj : object_lst3)
						em.persist(obj);
							
							for(CommonEntity obj : object_lst4)
								em.persist(obj);
	        
			t.commit();	
			//em.flush();
			
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}

	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#updated(jpa.entitys.common.CommonEntity)
	 * Date =Nov 12, 2013 : 11:30:36 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public CommonEntity updated(CommonEntity object)throws Exception {
		// TODO Auto-generated method stub
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();
	        t.begin();
	        
	        em.merge(object);
	        	
	        t.commit();
	        //em.flush();
	        
	        return object;
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#updated(java.util.List)
	 * Date =Nov 12, 2013 : 11:30:40 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public List<CommonEntity> updated(List<CommonEntity> object_lst) throws Exception{
		EntityTransaction t = null;
		try{
	        t = this.em.getTransaction();
	        t.begin();
	        
	        for(CommonEntity object : object_lst)
	        	em.merge(object);
	        
	        t.commit();
	        //em.flush();
	        
	        return object_lst;
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
	}


	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#deleted(jpa.entitys.common.CommonEntity)
	 * Date =Nov 12, 2013 : 11:30:45 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public CommonEntity deleted(CommonEntity object)throws Exception {
		return updated(object);
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#deleted(java.util.List)
	 * Date =Nov 12, 2013 : 11:30:49 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public List<CommonEntity> deleted(List<CommonEntity> object_lst) throws Exception
	{
		return updated(object_lst);
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#destroyed(jpa.entitys.common.CommonEntity)
	 * Date =Nov 12, 2013 : 11:30:53 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public void destroyed(CommonEntity object) throws Exception{
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();
	        t.begin();
	        
	        em.remove(object);
	        
	        t.commit();		
	        //em.flush();
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());   
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }
		
	}
	
	/**
	 * (Javadoc)
	 * @see jpa.service.common.JPAServiceIntrerface#destroyed(java.util.List)
	 * Date =Nov 12, 2013 : 11:30:56 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public void destroyed(List<CommonEntity> object_lst) throws Exception {
		EntityTransaction t = null;
		try{			
	        t = this.em.getTransaction();
	        t.begin();
	        for(CommonEntity object : object_lst)
	        	em.remove(object);
	        
	        t.commit();
	        //em.flush();
	        
		}catch(Exception e){
        	e.printStackTrace();
        	if (t.isActive()) 
				  t.rollback();
        	throw new Exception("Error:" + e.getMessage());
		}finally {
			  if (t.isActive()) 
				  t.rollback();
	    }		
	}
}
